package org.replay.config;
/*
 * Copyright © 2020 lizicong,All rights reserved.
 *
 * Licensed under the GPL License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 向MQ服务器注册自定义声明式的交换机,队列.并把两者绑定.
 * 该配置类不是必须的.只为了不需要到rabbitmq web 管理器上手动注册/绑定交换机和队列
 * @author lizicong
 */

@Configuration
public class RabbitBindConfiguration {

    private final static String myExchangeName = "seckill-exchange";
    private final static String myQueueName = "seckill-queue";
    private final static String myRouteKey = "seckill-routekey";

    public static String getMyExchangeName() {
        return myExchangeName;
    }

    public static String getMyQueueName() {
        return myQueueName;
    }

    public static String getMyRouteKey() {
        return myRouteKey;
    }

    /**
     * 自定义交换机
     *
     * @return DirectExchange
     */
    @Bean("my_exchange")
    public DirectExchange directExchange() {
        //durable:是否持久化？
        return new DirectExchange(myExchangeName, true, false);
    }

    /**
     * 自定义队列
     *
     * @return Queue
     */
    @Bean("my_queue")
    public Queue queue() {
        //durable:是否持久化？
        return new Queue(myQueueName, true);
    }

    /**
     * 绑定队列到交换机
     *
     * @param directExchange 自定义交换机
     * @param queue          自定义队列
     * @return Binding
     */
    @Bean
    Binding binding(@Qualifier("my_exchange") DirectExchange directExchange, @Qualifier("my_queue") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with(myRouteKey);
    }
}
